<?php

namespace app\admin\controller;



use app\admin\model\Region;
use app\admin\model\Store as modelStore;
use app\BaseController;

use think\response\Json;
use app\Request;
class Store extends BaseController
{

    protected $autoWriteTimestamp = 'datetime';

    public function storeList()
    {

        if(\request()->isAjax() && \request()->isGet()){

            $result=modelStore::getList();


            return $this->success($result);
        }


        return view('');

    }

    /****
     * @return Json
     */
    public function updateStatus(): Json
    {


        $save=input('');
        $result= (new \app\admin\model\Store)->where(['id'=>input('id')])->update($save);

        return $this->success();
    }

    public function storeDel(): Json
    {

        (new \app\admin\model\Store)->where(['id'=>input('id')])->delete();
        return $this->success();
    }

    public function editStore(request $request){

        $id= input('id');
        if($request->isPost() && $request->isAjax()){

            $id= input('id');

            empty($id) ? $type='add':$type='edit';
            if($type=='add'){
                $result= modelStore::insertCategory(input(''));
                return json($result);
            }else{

                $result= modelStore::modifyCategory(input(''));
                return json($result);
            }
        }

        $info=modelStore::where(['id'=>$id])->find();
        $info ? $info=$info->getData():$info=[];

        !empty(input('parent_id')) ? $parent_id=input('parent_id'):$parent_id=0;

        empty($id) ? $tpl='add_store':$tpl='edit_store';


        $region=Region::where(['level'=>1])->select()->toArray();

        $city=Region::where(['level'=>2])->select()->toArray();
        $area=Region::where(['level'=>3])->select()->toArray();
        $stores=modelStore::select();

        return \view($tpl,['info'=>$info,'region'=>$region,'city'=>$city,'area'=>$area,'stores'=>$stores]);
    }

    public function getCity(){


        $pid=input('pid');

        $region=Region::where(['parent_id'=>$pid])->select();

        return $this->success($region);
    }


}